import pygame
import sys

# 初始化Pygame
pygame.init()

# 设置窗口尺寸
WIDTH, HEIGHT = 900, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 模块概览")

# 颜色定义
BACKGROUND = (25, 25, 35)
TEXT_COLOR = (220, 220, 220)
HIGHLIGHT = (255, 215, 0)
MODULE_COLORS = [
    (255, 105, 97),   # 红色
    (255, 180, 128),  # 橙色
    (248, 243, 141),  # 黄色
    (66, 214, 164),   # 绿色
    (8, 202, 209),    # 青蓝色
    (89, 173, 246),   # 蓝色
    (157, 148, 255),  # 紫色
    (199, 128, 232),  # 紫红色
]

# 字体
title_font = pygame.font.SysFont(None, 60)
module_font = pygame.font.SysFont(None, 36)
desc_font = pygame.font.SysFont(None, 28)
small_font = pygame.font.SysFont(None, 24)

# Pygame 主要模块列表
pygame_modules = [
    {
        "name": "pygame.display",
        "description": "控制显示窗口和屏幕",
        "functions": ["set_mode()", "set_caption()", "flip()", "update()"]
    },
    {
        "name": "pygame.event",
        "description": "处理事件和输入",
        "functions": ["get()", "poll()", "Event", "EventType"]
    },
    {
        "name": "pygame.draw",
        "description": "绘制基本形状",
        "functions": ["rect()", "circle()", "line()", "polygon()"]
    },
    {
        "name": "pygame.image",
        "description": "图像加载和保存",
        "functions": ["load()", "save()", "fromstring()", "tostring()"]
    },
    {
        "name": "pygame.font",
        "description": "字体和文本渲染",
        "functions": ["Font", "SysFont()", "render()"]
    },
    {
        "name": "pygame.time",
        "description": "时间和帧率控制",
        "functions": ["Clock", "get_ticks()", "delay()", "set_timer()"]
    },
    {
        "name": "pygame.mixer",
        "description": "声音和音乐",
        "functions": ["Sound", "music", "Channel"]
    },
    {
        "name": "pygame.key",
        "description": "键盘输入处理",
        "functions": ["get_pressed()", "get_mods()", "name()"]
    },
    {
        "name": "pygame.mouse",
        "description": "鼠标输入处理",
        "functions": ["get_pos()", "get_pressed()", "set_visible()"]
    },
    {
        "name": "pygame.sprite",
        "description": "游戏精灵和组",
        "functions": ["Sprite", "Group", "GroupSingle", "collide_rect()"]
    },
    {
        "name": "pygame.transform",
        "description": "变换图像",
        "functions": ["scale()", "rotate()", "flip()", "rotozoom()"]
    },
    {
        "name": "pygame.surface",
        "description": "图像表面对象",
        "functions": ["Surface", "blit()", "convert()", "get_rect()"]
    },
    {
        "name": "pygame.rect",
        "description": "矩形区域处理",
        "functions": ["Rect", "colliderect()", "inflate()", "clip()"]
    },
    {
        "name": "pygame.math",
        "description": "数学工具",
        "functions": ["Vector2", "Vector3", "clamp()", "lerp()"]
    },
    {
        "name": "pygame.joystick",
        "description": "游戏手柄支持",
        "functions": ["Joystick", "get_count()", "get_init()"]
    }
]

# 主循环
clock = pygame.time.Clock()
scroll_offset = 0
scroll_speed = 20
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_DOWN:
                scroll_offset -= scroll_speed
            elif event.key == pygame.K_UP:
                scroll_offset += scroll_speed
        elif event.type == pygame.MOUSEWHEEL:
            scroll_offset += event.y * scroll_speed
    
    # 限制滚动范围
    scroll_offset = max(-(len(pygame_modules) * 80 - HEIGHT + 200), min(0, scroll_offset))
    
    # 填充背景
    screen.fill(BACKGROUND)
    
    # 绘制标题
    title = title_font.render("Pygame 主要模块", True, HIGHLIGHT)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 30))
    
    # 绘制说明
    instruction = desc_font.render("使用鼠标滚轮或上下箭头键滚动查看所有模块", True, TEXT_COLOR)
    screen.blit(instruction, (WIDTH//2 - instruction.get_width()//2, 90))
    
    # 绘制模块列表
    for i, module in enumerate(pygame_modules):
        y_pos = 150 + i * 80 + scroll_offset
        
        # 只绘制可见的模块
        if y_pos < HEIGHT and y_pos > -80:
            color = MODULE_COLORS[i % len(MODULE_COLORS)]
            
            # 绘制模块名称
            name_surface = module_font.render(module["name"], True, color)
            screen.blit(name_surface, (50, y_pos))
            
            # 绘制模块描述
            desc_surface = desc_font.render(module["description"], True, TEXT_COLOR)
            screen.blit(desc_surface, (50, y_pos + 40))
            
            # 绘制函数列表
            func_text = "函数: " + ", ".join(module["functions"])
            if len(func_text) > 80:  # 截断过长的文本
                func_text = func_text[:77] + "..."
            func_surface = small_font.render(func_text, True, (180, 180, 180))
            screen.blit(func_surface, (400, y_pos + 20))
    
    # 绘制模块总数
    count_text = f"总共展示了 {len(pygame_modules)} 个主要模块"
    count_surface = desc_font.render(count_text, True, HIGHLIGHT)
    screen.blit(count_surface, (WIDTH//2 - count_surface.get_width()//2, HEIGHT - 60))
    
    # 绘制退出提示
    exit_text = "按ESC退出"
    exit_surface = small_font.render(exit_text, True, TEXT_COLOR)
    screen.blit(exit_surface, (WIDTH - exit_surface.get_width() - 20, HEIGHT - 30))
    
    # 更新屏幕
    pygame.display.flip()
    clock.tick(60)

# 退出Pygame
pygame.quit()
sys.exit()